package com.chaoyi.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.config.SessionConfig;
import com.chaoyi.model.MenuTreeInfo;
import com.chaoyi.model.ModelInfo;
import com.chaoyi.model.Role;
import com.chaoyi.model.User;
import com.chaoyi.service.LoginService;
import com.chaoyi.service.ModelInfoService;
import com.chaoyi.service.RoleService;

@Controller
public class LoginController {
	@Resource
	private LoginService loginService;
	@Resource
	private RoleService roleService;
	@Resource
	private ModelInfoService  modelInfoService;
	
	@ResponseBody
	@RequestMapping("login")
	public JSONObject login(HttpServletRequest request, HttpServletResponse response,User user){
		JSONObject json = new JSONObject();
		List<User> userList =  loginService.findUser(user);
		if(userList.size() != 1){
			json.put("resc", 0001);
		}else{
			request.getSession().setAttribute(SessionConfig.USER, userList.get(0));
			json.put("resc", 0000);
		}
		return json;
	} 
	
    @RequestMapping("/logout")
    public void logout(HttpServletRequest req,HttpServletResponse resp) throws IOException{
        HttpSession session = req.getSession();
        session.invalidate();
        resp.sendRedirect(req.getContextPath()+"/login.jsp"); 
    }
	
    @RequestMapping("/menuTree")
    @ResponseBody
    public Map<String,List<MenuTreeInfo> > menuTree(HttpServletRequest request,HttpServletResponse response) throws Exception {
        Map<String,List<MenuTreeInfo> > resutMap = new HashMap<>();
        //返回的左侧菜单信息
        List<MenuTreeInfo> resultList = new ArrayList<>();
        User user = (User) request.getSession().getAttribute(SessionConfig.USER);
        //先查询出用户对应的角色、角色对应的菜单
        List<Role> roleList = roleService.findModelInfo(user.getRoleId());
        //查询出所有菜单后 筛选出角色对应的菜单
        List<ModelInfo> modelInfo = modelInfoService.findAllModelInfo();
        //存放用户对应的菜单
        List<ModelInfo> roleModel = new ArrayList<ModelInfo>();
        for(ModelInfo modAll:modelInfo){
        	for(Role role : roleList){
        		if(modAll.getModelId()==role.getModelId()){
        			roleModel.add(modAll);
        		}
        	}
        }
        //开始拼接左侧菜单
        //存放父菜单
        List<ModelInfo> modelList = new ArrayList<ModelInfo>();
        //存放子菜单
        List<ModelInfo> childList = new ArrayList<ModelInfo>();
        
        for(ModelInfo mod : roleModel){
        	if(mod.getParentId()==0){
        		modelList.add(mod);
        	}else{
        		childList.add(mod);
        	}
        }
        for(ModelInfo obj : modelList){
        	MenuTreeInfo parentMenu = new MenuTreeInfo();
        	parentMenu.setId(obj.getModelId());
        	parentMenu.setIcon("fa fa-circle-o");
        	parentMenu.setText(obj.getModelName());
        	//存放子菜单
        	List<MenuTreeInfo> childMenuList = new ArrayList<>();
        	for(ModelInfo child :childList){
        		if(child.getParentId()==obj.getModelId()){
        		   MenuTreeInfo cMenuTree = new MenuTreeInfo();
                   cMenuTree.setId(child.getModelId());
                   cMenuTree.setText(child.getModelName());
                   cMenuTree.setUrl(request.getContextPath().replace("/","")+child.getUrl());
                   cMenuTree.setTargetType("iframe-tab");
                   cMenuTree.setIcon("fa fa-table");
                   childMenuList.add(cMenuTree);
        		}
        	}
        	parentMenu.setChildren(childMenuList);
        	resultList.add(parentMenu);
        }    
        resutMap.put("rows",resultList);
        return  resutMap;
    }
}
